package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import Modelo.Musico;

public class daoMusico extends dao {
	
	public void inserirMusico(Musico musico) {

		String sql = "insert into musico (nome,ehSolo) values(?,?)";

		try {
			stmt = con.prepareStatement(sql);

			stmt.setString(1, musico.getNome());
			stmt.setBoolean(2, musico.isEhSolo());

			stmt.execute();
			stmt.close();

		}

		catch (SQLException e) {
			throw new RuntimeException(e);

		}

	}

	public void atualizarMusico(Musico musico) {

		String sql = "update musica set nome=? ,ehSolo=? where nome =?";

		try {
			stmt = con.prepareStatement(sql);

			stmt.setString(1, musico.getNome());
			stmt.setBoolean(2, musico.isEhSolo());

			stmt.execute();
			stmt.close();

		}

		catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public ArrayList<Musico> listarMusico() {

		String sql = "select * from musica";

		try {
			stmt = con.prepareStatement(sql);

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {

				Musico mu = new Musico();

				mu.setNome(rs.getString("nome"));
				mu.setEhSolo(rs.getBoolean("ehSolo"));
				Musico.add(mu);
			}
			return listarMusico();

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public ArrayList<Musico> buscarMusico(String campo, Musico mu) {
		try {
			ArrayList<Musico> mu1 = new ArrayList<>();
			String sql1 = "select * from musica where" + campo + "=?";

			stmt = con.prepareStatement(sql1);

			if (campo.equals("nome"))
				stmt.setString(1, mu.getNome());
			else if (campo.equals(" ehSolo"))
				stmt.setBoolean(1, mu.isEhSolo());

			ResultSet rs = stmt.executeQuery();

			while (rs.next()) {

				Musico mu2 = new Musico();

				mu2.setNome(rs.getString("nome"));
				mu2.setEhSolo(rs.getBoolean("ehSolo"));
				Musico.add(mu);

			}

			return mu1;

		} catch (SQLException e) {
			
			throw new RuntimeException(e);
		}
	}

	public void apagarMusico(Musico musico) {
		try {

			String sql1 = "delete from musica where nome =?";
			stmt = con.prepareStatement(sql1);
			stmt.setString(1, musico.getNome());

			stmt.executeUpdate(sql1);
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

}

